import requests
from bs4 import BeautifulSoup

# 淘宝商品页面 URL
url = 'https://www.chinaz.com/zmt/'
header = {
    "User-Agent":"Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Mobile Safari/537.36 Edg/116.0.1938.62"
}

response = requests.get(url=url,headers = header)
soup = BeautifulSoup(response.text, 'html.parser')

itemss = soup.find("ul",attrs={"id":"waterfall"})
items = itemss.findAll("a",attrs={"rel":"noopener noreferrer"})
i = 1
for item in items:

    href = item.get('href')
    if href.endswith('shtml'):
        link_res = requests.get(href)
        soup1 = BeautifulSoup(link_res.text,'html.parser')
        item1 = soup1.find("div",attrs={"id":"article-content"})
        item2 = item1.findAll("p")
        for item3 in item2 :
            filename = 'page/'+str(i) + '.txt'
            file = open(filename,'a', encoding='utf-8')
            file.write(item3.text+'\n')
        print("第"+str(i)+"个文件创建成功")
        i+=1

